﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace LIMS.Shared
{
    public class DepartmentManagementPresenter : BasePresenter<IDepartmentManagementPageView>
    {
        private IDepartmentManagementPageView view;
       
    
        private IUserManagementService userService;
        private IPlantManagementService plantService;
        private IDepartmentManagementService deparatmentservice;
        private IGroupManagementService groupService;        
        private ILogger logger;
        /// <summary>
        /// Constructor of UserManagementPagePresenter
        /// </summary>
        /// <param name="view"></param>
        /// <param name="oLogger"></param>
        /// <param name="myService"></param>
        /// <param name="oGroupService"></param>
        public DepartmentManagementPresenter(IDepartmentManagementPageView view,ILogger oLogger, 
            IUserManagementService myService, IPlantManagementService plantService,IDepartmentManagementService deparatmentservice,
            IGroupManagementService oGroupService)
            : base(view)
        {
            this.view = view;           
            this.logger = oLogger;
            this.userService = myService;
            this.groupService = oGroupService;
            this.plantService = plantService;
            this.deparatmentservice = deparatmentservice;
        }
        /// <summary>
        /// Initializing user view 
        /// </summary>
        /// <returns></returns>
        public bool InitializeView()
        {                       
            try
            {
                GridViewData temp = view.GridData;
                temp.Criteria.Clear();
                temp.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = temp;
                view.Departments = deparatmentservice.GetAllDepartmentForGrid(ref oGrdData);
                view.GridData = oGrdData;                
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error while initializing plants view", ex);
                return false;

            }
        }
        /// <summary>
        /// Save User
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public bool Save(Department newDept)
        {
            try
            {
                bool bIsNewUser = (newDept.Id == 0) ? true : false;
                if (deparatmentservice.Save(newDept))
                {
              
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error while saving department" + newDept.Name.ToString() + " information.", ex);
                return false;
            }
        }
        /// <summary>
        /// Delete User / Deactivate User
        /// </summary>
        /// <param name="dUser"></param>
        /// <returns></returns>
        public bool Delete(Department dDept)
        {
            dDept.Status = "D";
            return deparatmentservice.Save(dDept);
        }

        /// <summary>
        /// Edits a product, setter does the trick.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Edit(int index)
        {
            try
            {
                view.CurrentIndex = index;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error occured while editing record. Exception : " + ex.Message, ex);
                return false;
            }
        }

       
             /// <summary>
        /// Search plant
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public void SearchDepartmentByKeyword(string sKeyword)
        {
            try
            {
                view.GridData.Criteria.Clear();
                view.GridData.Criteria.Add(new GVEntityData("Name", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.Or));
                view.GridData.Criteria.Add(new GVEntityData("Location", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.And));
                view.GridData.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = view.GridData;
                List<Department > myDepartments = deparatmentservice.GetAllDepartmentForGrid(ref oGrdData);
                view.GridData = oGrdData;
                view.Departments = myDepartments;
            }
            catch (Exception ex)
            {
                logger.Error("Error in Searching user using Keyword for :" + sKeyword, ex);
            }
        
        }

         /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Plant> GetAllPlants()
        {

            //List<string> oListCounties = new List<string>();
            List<Plant> oPlantList = new List<Plant>();
            try
            {
                oPlantList = plantService.GetAllPlants(); 
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting counties " + ex.ToString());
            }
            return oPlantList;

        }
            
        
    }
}
